/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.maning.mnprogressdialog.slice;

import com.maning.mndialoglibrary.MProgressBarDialog;
import com.maning.mndialoglibrary.MProgressDialog;
import com.maning.mndialoglibrary.MStatusDialog;
import com.maning.mndialoglibrary.MToast;
import com.maning.mndialoglibrary.config.MDialogConfig;
import com.maning.mndialoglibrary.config.MToastConfig;
import com.maning.mndialoglibrary.listeners.OnDialogDismissListener;
import com.maning.mnprogressdialog.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.multimodalinput.event.KeyEvent;

import java.io.IOException;

import static com.maning.mndialoglibrary.MProgressBarDialog.MProgressBarDialogStyle_Circle;
import static com.maning.mndialoglibrary.MProgressBarDialog.MProgressBarDialogStyle_Horizontal;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private static final String text01 = "从前有坐山,山上有坐庙,庙里有个老和尚在讲故事,讲的什么啊,从前有座山,山里有座庙,庙里有个盆,盆里有个锅,锅里有个碗,碗里有个匙,匙里有个花生仁,我吃了,你谗了,我的故事讲完了.";
    private Button btn01;
    private Button btn02;
    private Button btn03;
    private Button btn04;
    private Button btn05;
    private Button btn06;
    private Button btn07;
    private Button btn08;
    private Button btn09;
    private Button btn10;
    private Button btn11;
    private Button btn12;
    private Button btn13;
    private Button btn14;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    public void initView(){
        btn01=(Button) findComponentById(ResourceTable.Id_btn01);
        btn02=(Button) findComponentById(ResourceTable.Id_btn02);
        btn03=(Button) findComponentById(ResourceTable.Id_btn03);
        btn04=(Button) findComponentById(ResourceTable.Id_btn04);
        btn05=(Button) findComponentById(ResourceTable.Id_btn05);
        btn06=(Button) findComponentById(ResourceTable.Id_btn06);
        btn07=(Button) findComponentById(ResourceTable.Id_btn07);
        btn08=(Button) findComponentById(ResourceTable.Id_btn08);
        btn09=(Button) findComponentById(ResourceTable.Id_btn09);
        btn10=(Button) findComponentById(ResourceTable.Id_btn10);
        btn11=(Button) findComponentById(ResourceTable.Id_btn11);
        btn12=(Button) findComponentById(ResourceTable.Id_btn12);
        btn13=(Button) findComponentById(ResourceTable.Id_btn13);
        btn14=(Button) findComponentById(ResourceTable.Id_btn14);

        btn01.setClickedListener(this);
        btn02.setClickedListener(this);
        btn03.setClickedListener(this);
        btn04.setClickedListener(this);
        btn05.setClickedListener(this);
        btn06.setClickedListener(this);
        btn07.setClickedListener(this);
        btn08.setClickedListener(this);
        btn09.setClickedListener(this);
        btn10.setClickedListener(this);
        btn11.setClickedListener(this);
        btn12.setClickedListener(this);
        btn13.setClickedListener(this);
        btn14.setClickedListener(this);

    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()){
            case ResourceTable.Id_btn01:
                MProgressDialog.showProgress(this,"",null,1000);
                getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                    @Override
                    public void run() {
                        MProgressDialog.showProgress(getContext(),"加载中...",null,2000);
                    }

                }, 1000);

                break;
            case ResourceTable.Id_btn02:
                MProgressDialog.showProgress(this,text01,null,3000);

                break;
            case ResourceTable.Id_btn03:
                MProgressDialog.showProgress(this,"",null,3000);
                break;
            case ResourceTable.Id_btn04:
                MDialogConfig mDialogConfig2 = new MDialogConfig.Builder()
                        //全屏模式
                        .isWindowFullscreen(true)
                        .isCanceledOnTouchOutside(true)
                        .isCancelable(true)
                        .setProgressSize(128)
                        .setProgressColor(Color.GREEN)
                        .setProgressRimColor(Color.YELLOW)
                        .setProgressRimWidth(4)
                        .setProgressWidth(8)
                        .setTextColor(Color.BLUE)
                        .setMinWidthAndHeight(50,50)
                        .setPadding(16,16,16,16)
                        .setOnDialogDismissListener(new OnDialogDismissListener() {
                            @Override
                            public void onDismiss() {

                            }
                        })
                        .build();
                MProgressDialog.showProgress(this,"数据上传中...",mDialogConfig2);
                break;
            case ResourceTable.Id_btn05:
                MStatusDialog statusDialog =new MStatusDialog(this);
                PixelMapElement  pixelMapElement = null;
                try {
                    ResourceManager manager = getResourceManager();
                    pixelMapElement = new PixelMapElement(manager.getResource(ResourceTable.Media_mn_icon_dialog_ok));
                    statusDialog.show("正在保存，请稍等...",pixelMapElement,3000);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NotExistException e) {
                    e.printStackTrace();
                }
                if(pixelMapElement!=null){
                    PixelMapElement finalPixelMapElement = pixelMapElement;
                    getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                        @Override
                        public void run() {
                            MStatusDialog statusDialog =new MStatusDialog(getContext());
                            statusDialog.show("保存成功", finalPixelMapElement,1000);
                        }

                    }, 3000);
                }




                break;
            case ResourceTable.Id_btn06:
                MDialogConfig mDialogConfig = new MDialogConfig.Builder()
                        //全屏模式
                        .isWindowFullscreen(true)
                        //全屏背景窗体的颜色
                        //.setBackgroundWindowColor(getMyColor(R.color.colorDialogWindowBg))
                        //View背景的颜色
                        //.setBackgroundViewColor(getMyColor(R.color.colorDialogViewBg2))
                        //字体的颜色
                        .setTextColor(Color.RED)
                        //文字大小
                        .setTextSize(36)
                        //View边框的颜色
                        //.setStrokeColor(getMyColor(R.color.white))
                        //View边框的宽度
                        .setStrokeWidth(2)
                        //View圆角大小
                        .setCornerRadius(10)
                        //动画
                        //.setAnimationID(R.style.animate_dialog_custom)
                        //图片的宽高dp
                        .setImgWidthAndHeight(128, 128)
                        //padding
                        .setPadding(20, 10, 20, 10)
                        //关闭的监听
                        .setOnDialogDismissListener(new OnDialogDismissListener() {
                            @Override
                            public void onDismiss() {
                                MToast.makeTextShort(getContext(), "监听到了MStatusDialog关闭了",new MToastConfig());
                            }
                        })
                        .build();


                try {
                    ResourceManager manager = getResourceManager();
                    PixelMapElement pixelMapElement3 = new PixelMapElement(manager.getResource(ResourceTable.Media_icon_staues_test));
                    new MStatusDialog(getContext(), mDialogConfig)
                            .show("恭喜你，签到成功\n积分+10",
                                    pixelMapElement3,
                                    1500);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NotExistException e) {
                    e.printStackTrace();
                }


                break;
            case ResourceTable.Id_btn07:
                MToastConfig config0 = new MToastConfig.Builder()
                        .setTextSize(30)
                        //.setTextColor(Color.LTGRAY)
                        //.setBackgroundColor(new RgbColor(2,212,166))
                        .build();
                MToast.makeTextShort(getContext(), "我是默认Toast",config0);

                break;
            case ResourceTable.Id_btn08:
                try {
                    ResourceManager manager = getResourceManager();
                    PixelMapElement pixelMapElement4 = new PixelMapElement(manager.getResource(ResourceTable.Media_mn_icon_dialog_ok));
                    MToastConfig config = new MToastConfig.Builder()
                            .setTextSize(48)
                            .setBackgroundColor(new RgbColor(2,212,166))
                            .setToastIcon(pixelMapElement4)
                            .build();
                    MToast.makeTextShort(getContext(), "欢迎使用自定义Toast-异步线程",config);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (NotExistException e) {
                    e.printStackTrace();
                }

                break;
            case ResourceTable.Id_btn09:
                MToastConfig config = new MToastConfig.Builder()
                        .setTextSize(36)
                        .setTextColor(Color.RED)
                        .setBackgroundColor(new RgbColor(2,212,166))
                        .build();
                MToast.makeTextShort(getContext(), text01,config);
                break;
            case ResourceTable.Id_btn10:
                MToastConfig config2 = new MToastConfig.Builder()
                        .setTextSize(36)
                        .build();
                MToast.makeTextShort(getContext(), "从前有坐山,山上有坐庙,庙里有个老和尚在讲故事 \n 讲的什么啊,从前有座山,山里有座庙,庙里有个盆",config2);
                break;
            case ResourceTable.Id_btn11:
                //新建一个Dialog
                mProgressBarDialog = new MProgressBarDialog.Builder(getContext())
                        .setStyle(MProgressBarDialogStyle_Horizontal)
                        .build();
                startProgress(false);
                break;
            case ResourceTable.Id_btn12:
                mProgressBarDialog = new MProgressBarDialog.Builder(getContext())
                        .setProgressColor(Color.GREEN)
                        .setProgressbarBackgroundColor(Color.BLUE)
                        .setHorizontalProgressBarHeight(16)
                        .setStyle(MProgressBarDialogStyle_Horizontal)
                        .setTextColor(Color.RED)
                        .build();
                startProgress(false);
                break;
            case ResourceTable.Id_btn13:
                mProgressBarDialog = new MProgressBarDialog.Builder(getContext())
                        .setStyle(MProgressBarDialogStyle_Circle)
                        .setTextColor(Color.RED)
                        .build();
                startProgress(false);
                break;
            case ResourceTable.Id_btn14:
                mProgressBarDialog = new MProgressBarDialog.Builder(getContext())
                        .setProgressColor(Color.GREEN)
                        .setProgressbarBackgroundColor(Color.BLUE)
                        .setHorizontalProgressBarHeight(16)
                        .setCircleProgressBarWidth(8)
                        .setCircleProgressBarBackgroundWidth(4)
                        .setStyle(MProgressBarDialogStyle_Circle)
                        .setTextColor(Color.RED)
                        .build();
                startProgress(false);
                break;
        }
    }
    MProgressBarDialog mProgressBarDialog;
    //当前进度
    private int currentProgress;

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (currentProgress < 100) {
                mProgressBarDialog.showProgress(currentProgress, "当前进度为：" + currentProgress + "%", false);
                currentProgress += 1;
                getContext().getUITaskDispatcher().delayDispatch(runnable, 200);
            } else {
                currentProgress=0;
                mProgressBarDialog.showProgress(100, "完成",false);
                getContext().getUITaskDispatcher().delayDispatch(new Runnable() {
                    @Override
                    public void run() {
                        mProgressBarDialog.dismiss();
                    }
                }, 1000);
            }
        }
    };

    private void startProgress(boolean animal) {
        getContext().getUITaskDispatcher().delayDispatch(runnable, 0);
    }
}
